package com.gton.person.commons.hander;

import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: PersonalDesign
 * @description: 存储验证码的容器
 * 如果需要判断验证码直接修改是成本比较大的方式，可以新增AuthenticationProvider来对验证码新增验证
 * @author: GuoTong
 * @create: 2020-11-30 15:10
 **/
public class CaptchaAuthenticationDetails extends WebAuthenticationDetails {
    //验证码字段
    private final String DEFAULT_CAPTCHA_PARAMETER_NAME = "captcha";

    //保存的验证码字段
    private String captchaParameter = DEFAULT_CAPTCHA_PARAMETER_NAME;
    /**
     * 用户提交的验证码
     */
    private String committedCaptcha;
    /**
     * 预设的验证码
     */
    private String presetCaptcha;

    //常量
    private final WebAuthenticationDetails webAuthenticationDetails;

    public CaptchaAuthenticationDetails(HttpServletRequest request) {
        super(request);
        this.committedCaptcha = request.getParameter(captchaParameter);
        this.webAuthenticationDetails = new WebAuthenticationDetails(request);
    }
    public boolean isCaptchaMatch() {
        if (this.presetCaptcha == null || this.committedCaptcha == null) {
            return false;
        }
        return this.presetCaptcha.equalsIgnoreCase(committedCaptcha);
    }

    public String getCaptchaParameter() {
        return captchaParameter;
    }

    public void setCaptchaParameter(String captchaParameter) {
        this.captchaParameter = captchaParameter;
    }

    public String getCommittedCaptcha() {
        return committedCaptcha;
    }

    public void setCommittedCaptcha(String committedCaptcha) {
        this.committedCaptcha = committedCaptcha;
    }

    public String getPresetCaptcha() {
        return presetCaptcha;
    }

    public void setPresetCaptcha(String presetCaptcha) {
        this.presetCaptcha = presetCaptcha;
    }

    public WebAuthenticationDetails getWebAuthenticationDetails() {
        return webAuthenticationDetails;
    }
}
